<?php
/**
 +------------------------------------------------------------------------------
 * @author   heihei2202 <admin@sameus.com>
 * @version    2012-05-15 04:59:14Z heihei2202 $
 +------------------------------------------------------------------------------
 */
// 本类由系统自动生成，仅供测试用途
class ArticleAction extends CenterAction {
   public function index(){
       //查询加入的群组名字
       $group=M('Groups');
       $grouplist=$group->where(array('user_id'=>$_SESSION['uid']))->select();
       //dump($grouplist);
       $this->assign('group_list',$grouplist);
     //  dump($grouplist);
       $this->display('choose_article');
    }   
    public function article(){
       //查询选择的群的关键字,若为保存草稿,则不需要查询
       if($_GET['gid']){
           $gid=$_GET['gid'];
           
           $keyword=D('KeywordView');
           $keydata['main_id']=$gid;
           $keydata['type']='groups';
           $keylist=$keyword->where($keydata)->field('main_id,id,key_name,key_count')->group('key_name')->select();
           $this->assign('key_list',$keylist);
           $this->assign('gid',$gid);
            $this->assign('gname',$_GET['gname']);
       } 
      // dump($keylist);
       $this->display('article');
    }
    
     public function article_insert(){
         $article=D('Article'); 
         $keyword=M('Keyword');
         $deliver=M('Deliver');// 
         $relation=M('keyword_relation');
         $gid=$_POST['group_id'];
         //验证转载链接是否为空
         $rule = '/^http:\/\/[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^<>\"\"])*$/';
         if($_POST['type']==2){
             if($_POST['link']=='' || preg_match($rule,$_POST['link'])==false ){
                $this->error('转载文章链接不正确!'); 
             }
          }
         if($data=$article->create()){
             $data['uid']=$_SESSION['uid'];
             $data['uname']=$_SESSION['uname'];
             if($_POST['aid']){
               $data['aid']=$_POST['aid'];
                $aid=$article->save($data);
             }else{
                  $data['create_time']=date('Y-m-d H:i:m',time());
                 $aid=$article->add($data);
             }
             
                 //插入与文章相关的的关键字
                 foreach($_POST['key_word'] as $key=>$value){
                   if($value!=null){
                         $keydata['key_name']=$value; 
                         $rela_data['main_id']=$aid;
                         if($keyid=$keyword->where($keydata)->field('id')->find()){
                             $rela_data['keyword_id']=$keyid['id'];
                         }elseif($keyid=$keyword->add($keydata)){
                             $rela_data['keyword_id']=$keyid;
                         }
                         $rela_data['type']='article';//查询需要用article
                         $re_list=$relation->where($rela_data)->find();
                         if($re_list == false){
                            $re_id= $relation->add($rela_data);//添加关键字到文章和关键字的关系表
                         }
                   }  
                 }
                 
                 //自动推荐文章到相关群组,投稿
                  $datas['user_id']=$_SESSION['uid'];
                 // $datas['user_name']=$_SESSION['uname'];
                  $datas['deliver_id']=$aid;
                  $datas['state']=2;
                  $datas['for_id']=$gid;
                  $datas['kind']=1;
                  if($deliver->add($datas)){
                    $this->assign("jumpUrl",'__APP__/Group/article?aid='.$aid.'&gid='.$gid);
                     $this->success('成功');
                  }else{
                       $this->error('文章已保存,但投稿失败,请至个人中心查看');
                  }
          
         }else{
            $this->error($article->getError());
         }
         
     }
     
     public function my_article(){
        $share=M('Article');
        $data['uid']=$_SESSION['uid'];
        $data=$share->where($data)->field('aid,uid,group_id,create_time,uname,name,group_name,use_count,useless_count,state')->select();
        //dump($data);
        $this->assign('article_list',$data);
         $this->display();      
     }
     public function edit_article(){
       //查询选择的群的关键字,若为保存草稿,则不需要查询
         $aid=$_GET['aid'];
       if($_GET['aid']){
           $gid=$_GET['gid'];
           $keyword=D('KeywordView');
           $keydata['main_id']=$aid;
           $keydata['type']='article';
           $keylist=$keyword->where($keydata)->field('main_id,id,key_name,key_count')->group('key_name')->select();
           $this->assign('editkey_list',$keylist);
           $this->assign('gid',$gid);
       } 
       $article=M('Article'); 
       $group_list=$article->where(array('aid'=>$aid))->find();
       // dump($group_list);
       $this->assign('group_list',$group_list);
       $this->display('article');
    }
     public function article_comment(){
         $comment=M('Comment');
         if($_POST['comment_content']!='' && $data=$comment->create()){
             $data['user_id']=$_SESSION['uid'];
             $data['user_name']=$_SESSION['uname'];
             $id=$comment->add($data);
             
             $this->success('评论成功!');
         }else{
             $this->error('评论不能为空!');
         }
         $this->display();
    }
    /*
     * 发表导读-短文
     */
    public function shortarticle(){
        $this->display('Group:articleshort-add');
    }
    public function shortarticle_insert(){
        //先加入文章表,记录id,统计数据,若成功,加入$sucess,都则加入$error
       $artlist=$_POST['art'];
       foreach ($artlist as $key => $value) {
          $result= $this->insertart($value);
          if($result != FALSE){
              $sucess = $result;
          }
       }
    }
    protected function insertart($value){
         $art=M('Article'); 
         $data['']= $value[''];
         if($id=$art->add($data)){
             return $id;
         }else{
             return FALSE;
         } 
    }
}